/**
 * 
 */
//angular 路由
angular.module('main', ['ui.router', 'ngCookies']).config(['$stateProvider', '$urlRouterProvider', function (stateProvider, urlRouterProvider) {
        urlRouterProvider.otherwise('/');
        stateProvider.state('default', {
            url: '/main',
            templateUrl: 'view/out/body/views.html'
        }).state('main', {
            url: '/main',
            templateUrl: 'view/out/body/views.html'
        }).state('register', {
            url: '/register',
            templateUrl: 'view/out/body/register.html'
        }).state('forget', {
            url: '/forget',
            templateUrl: 'view/out/body/getPassword.html'
        });
    }]).service('$zb-service', function () {
    	this.putResult=function(options){
    		if(options.data.type==="result" && options.data.status===100)
    			options.successCallback(options.data);
    		else if(options.data.type==="result" && options.data.status===200)
    			options.errorCallback(options.data);
    		else if(options.data.type==="exception")
    			options.exceptionCallback(options.data);
    	};    	
}).controller('login', ['$scope', '$rootScope', '$cookies', '$cookieStore', '$http', '$rootScope', function ($scope, $rootScope, $http, $cookies, $cookieStore, $rootScope) {
        $scope.parameter = Math.random() * 10;
        $scope.random = function () {
            $scope.parameter = Math.random() * 10;
        };
        $scope.user = {email: $cookies.get("email"), password: $cookies.get("password")};
        $scope.isActive = false;
        $scope.active = function () {
            $scope.isActive = true;
        };
        $(document).ready(
                function () {
                    $("#zb-login").validate({
                        submitHandler: function (form) {
//                            alert($.param($scope.user));
//                            $.post('')
                        },
                        rules: {
                            email: {
                                required: true,
                                email: true
                            },
                            password: {required: true,
                                rangelength: [6, 20]},
                            verify: {required: true},
                        },
                        errorElement: "em",
                        errorPlacement: function (error, element) {
                            $("em").remove();
                            // Add the `help-block` class to the error element
                            error.addClass("help-block");
                            // Add `has-feedback` class to the parent div.form-group
                            // in order to add icons to inputs
                            element.parents(".col-sm-5").addClass("has-feedback");

                            if (element.prop("type") === "checkbox") {
                                error.insertAfter(element.parent("label"));
                            } else {
                                error.insertAfter(element);
                            }

                            // Add the span element, if doesn't exists, and apply the icon classes to it.
                            if (!element.next("span")[ 0 ]) {
                                $("<span class='glyphicon glyphicon-remove form-control-feedback'></span>").insertAfter(element);
                            }
                        },
                        success: function (label, element) {
                            $("em").remove();
//                            $(element).remove();
                            // Add the span element, if doesn't exists, and apply the icon classes to it.
                            if (!$(element).next("span")[ 0 ]) {
                                $("<span class='glyphicon glyphicon-ok form-control-feedback'></span>").insertAfter($(element));
                            }
                        },
                        highlight: function (element, errorClass, validClass) {
                            $(element).parents(".col-sm-5").addClass("has-error").removeClass("has-success");
                            $(element).next("span").addClass("glyphicon-remove").removeClass("glyphicon-ok");
                        },
                        unhighlight: function (element, errorClass, validClass) {
                            $(element).parents(".col-sm-5").addClass("has-success").removeClass("has-error");
                            $(element).next("span").addClass("glyphicon-ok").removeClass("glyphicon-remove");
                        }
                    }
                    );
                }
        );
    }]).controller('forget', ['$scope', function ($scope) {
        $scope.send = function () {
            $.post("outLogin/Pubgetpassword", {email: $scope.email}, function (msg) {
                alert(msg);
                if (msg.status === 100) {
                    $.growl.notice({
                        title: "",
                        message: msg.message
                    });
                } else {
                    $.growl.error({
                        title: "",
                        message: msg.message
                    });
                }
            });
        };
    }]).controller('register', ['$scope', '$zb-service', function ($scope, $zb_service) {
        $scope.isActive = false;
        $scope.parameter = Math.random() * 10;
        $scope.random = function () {
        $scope.parameter = Math.random() * 10;
        };
        $("input[name=headImg]").change(function () {
            var fileObj = $(this)[0].files[0];           
            var windowURL = window.URL || window.webkitURL;
            if (fileObj) {            	
                var dataURL = windowURL.createObjectURL(fileObj);                
                $(this).parent()                
                .next()
                .children('img')
                .attr('src', dataURL);
            }
        });
        $scope.active = function () {
            $scope.isActive = true;
        };
        //注册界面的表单验证	
        $(document).ready(
                function () {
                    $("#zb-register").validate({
                        submitHandler: function (form) {
                            var formData = new FormData(form);                            
                            $.ajax({
                                url: 'init/Pubregister',
                                type: 'POST',
                                data: formData,
                                // 告诉jQuery不要去处理发送的数据
                                processData: false,
                                // 告诉jQuery不要去设置Content-Type请求头
                                contentType: false,
                                success: function (msg) {      
                                	$zb_service.putResult({
                                		data:msg,
                                		successCallback:function(data){
                                			 $.growl.notice({
                                                 title: "恭喜",
                                                 message: msg.data.message
                                             });
                                		},
                                		exceptionCallback:function(data){
                                			alert(angular.toJson(data));
                                			$scope.result=data;
                                			$scope.$apply();  
                                			},
                                		errorCallback:function(data){ 
                                			$scope.result=data;
                                			$scope.$apply();  
                                			}
                                	});
                                },
                                error: function (data) {
                                    if(data.statusCode===404)
                                    	alert("服务器地址错误！");                                   
                                }
                            });
                        },
                        rules: {
                            account: {
                                required: true,
                                isAccount: true,
                                rangelength: [6, 12]
                            },
                            password: {
                                required: true,
                                isCharOrNumber: true,
                                rangelength: [6, 18]},
                            okpassword: {required: true,
                                //登录界面和注册界面有2个name=password的input，再去取注册界面的
                                equalTo: "input[name=password]:eq(1)"},
                            email: {
                                required: true,
                                email: true,
                                remote: "init/Pubverify.json"
                            },
                            qq: {
                                required: true,
                                isQQ: true
                            },
                            phone: {
                                required: true,
                                isMobile: true
                            },
                            headImg: {
                                required: true,
                                checkPic: true,
                                checkPicSize: true
                            },
                            name: {
//                                isName: true,
//                                rangelength: [1, 8]
                            },
                            verify: {required: true}
                        },
                        messages: {
                            email: {
                                remote: "邮箱已使用，请更换邮箱！"
                            },
                            okpassword: {
                                equalTo: "两次密码不一致"
                            }
                        },
                        errorElement: "em",
                        errorPlacement: function (error, element) {
                            // Add the `help-block` class to the error element
                            error.addClass("help-block");

                            // Add `has-feedback` class to the parent div.form-group
                            // in order to add icons to inputs
                            element.parent("div").addClass("has-feedback");

                            if (element.prop("type") === "checkbox") {
                                error.insertAfter(element.parent("label"));
                            } else {
                                error.insertAfter(element);
                            }

                            // Add the span element, if doesn't exists, and apply the icon classes to it.
                            if (!element.next("span")[ 0 ]) {
                                if (element.attr('type') === 'file')
                                    $("<span class='glyphicon glyphicon-remove form-control-static text-danger'></span>").insertAfter($(element));
                                else
                                    $("<span class='glyphicon glyphicon-remove form-control-feedback'></span>").insertAfter($(element));
                            }
                        },
                        success: function (label, element) {
                            // Add the span element, if doesn't exists, and apply the icon classes to it.
                            if (!$(element).next("span")[ 0 ]) {
                                $("<span class='glyphicon glyphicon-ok form-control-feedback'></span>").insertAfter($(element));
                            }
                        },
                        highlight: function (element, errorClass, validClass) {
                            $(element).parent("div").addClass("has-error").removeClass("has-success");
                            if ($(element).attr('type') === 'file')
                                $(element).next("span").addClass("glyphicon-remove").removeClass("glyphicon-ok").removeClass("text-success").addClass("text-danger").text("头像上传错误");
                            else
                                $(element).next("span").addClass("glyphicon-remove").removeClass("glyphicon-ok");
                        },
                        unhighlight: function (element, errorClass, validClass) {
                            $(element).parent("div").addClass("has-success").removeClass("has-error");
                            if ($(element).attr('type') === 'file')
                                $(element).next("span").addClass("glyphicon-ok").removeClass("glyphicon-remove").removeClass("text-danger").addClass("text-success").text("头像上传正确");
                            else
                                $(element).next("span").addClass("glyphicon-ok").removeClass("glyphicon-remove");
                        }
                    });
                });
    }]);







//  	controller('navbar',['$scope','$rootScope','$http',function($scope,$rootScope,$http){
//  			 //$scope.user={name:''}; 		
//  	      //  $http.get('/someUrl', config).then(successCallback, errorCallback);
//  	
//  	}]).
//  	controller('login',['$scope','$rootScope','$cookies','$cookieStore','$http',function($scope,$rootScope,$http,$cookies,$cookieStore){ 	  		      	  		      				  		 		  
//  		          $scope.user={email:$cookies.get("email"),password:$cookies.get("password")};
//  		          //  $rootScope.user.password=$cookies.get("password");  				
//				$(document).ready(	
//		  				function(){
//		  					$("#zb-login").validate({
//		  					  submitHandler:function(form){	
//		  						  alert($.param($scope.user));
//		  						$.post('')
//		  					 },
//		  					 	 rules:{			 												
//		  						email:{
//		  									required:true,
//		  									email:true,							
//		  									},
//		  						password:{required:true,
//		  								    rangelength:[6,20]},					
//		  					    verify:{required:true,},
//		  					},			  			
//		  					errorElement: "em",
//		  						errorPlacement: function ( error, element ) {	
//		  						$( "em" ).remove(); 			
//		  							// Add the `help-block` class to the error element
//		  							error.addClass( "help-block" );
//		  							// Add `has-feedback` class to the parent div.form-group
//		  							// in order to add icons to inputs
//		  							element.parents( ".col-sm-5" ).addClass( "has-feedback" );
//		
//		  							if ( element.prop( "type" ) === "checkbox" ) {
//		  								error.insertAfter( element.parent( "label" ) );
//		  							} else {
//		  								error.insertAfter( element );
//		  							}
//		
//		  							// Add the span element, if doesn't exists, and apply the icon classes to it.
//		  						//	if ( !element.next( "span" )[ 0 ] ) {
//		  						//		$( "<span class='glyphicon glyphicon-remove form-control-feedback'></span>" ).insertAfter( element );
//		  						//	}
//		  						},
//		  						success: function ( label, element ) {	
//		  						    $( "em" ).remove(); 				
//		  							// Add the span element, if doesn't exists, and apply the icon classes to it.
//		  							if ( !$( element ).next( "span" )[ 0 ] ) {
//		  								$( "<span class='glyphicon glyphicon-ok form-control-feedback'></span>" ).insertAfter( $( element ) );
//		  							}
//		  						},
//		  						highlight: function ( element, errorClass, validClass ) {
//		  							$( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
//		  							$( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
//		  						},
//		  						unhighlight: function ( element, errorClass, validClass ) {				
//		  							$( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
//		  							$( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
//		  						}
//		  					
//		  					}
//		  						);
//		  				}
//		  				);
//		  			
//  	}])
//  	;	
//
////验证码点击更换
//
// $(".zb-verify").click(function(){$(this).attr("src","HelpTool/verify/" + Math.random() * 10);});
////显示 input  图片到img
//    $("input[type=file]").change(function(){$("div[div=head]").append("<img class='pull-right img-thumbnail '  width='50' height='50'  />");});
//	//当文件input改变时，将图片URL传到第一个img
//	  $("input[name=headImg]").change(function () {
//	  	  	var fileObj = $('input[name=headImg]')[0];
//	   	    var file = fileObj.files[0];
//	        var windowURL = window.URL || window.webkitURL;
//	        var dataURL;
//	        var $img = $("img:first");
//	     if (fileObj && fileObj.files && fileObj.files[0]) {
//	         dataURL = windowURL.createObjectURL(file);
//	      	$img.attr('src', dataURL);} 	  
//	  });
//

//登录界面的表单验证	  


//	
//   var loading= "<div id='loading' > \
//	   <div style='width:100%;height:100%;top:0px;left:0px;position:fixed;background:#636363;opacity:0.9;z-index:998;'></div>\
//	   <div><img src='images/loading.gif' style='width:3rem;position:absolute;left:50%;top:50%;z-index:999;margin-left:-5rem;margin-top:-5rem;' /></div>\
//	</div>";
//	$(document).ajaxStart(function(){	
//		$(loading).appendTo('body');		
//	}).ajaxStop(function() {	
//		$("#loading").remove();
//		});